import pymysql
import time
from datetime import datetime
from fastapi import Request
host = 'localhost'
user = 'root'
port = 3306
db = '340'
password = '123456'

name_mapping = {
    'admin': '超级管理员',
    'chen': '陈一',
    'zhangsan': '张三',
    'zeng': '曾',
    # 可以继续添加更多的映射...
}


def get_connection():
    conn = pymysql.connect(host=host, port=port, db=db, user=user, password=password)
    return conn


def get_data():
    timestamp = time.time()
    local_time = time.localtime(timestamp)
    formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
    return formatted_time

def get_client_ip(request: Request) -> str:
    # FastAPI的Request对象也提供了一个client.host属性，它通常包含客户端的IP地址
    # 但是，如果请求是通过代理（如Nginx）转发的，您可能需要查看X-Forwarded-For头
    ip_address = request.client.host
    x_forwarded_for = request.headers.get('X-Forwarded-For')
    if x_forwarded_for:
        # X-Forwarded-For头可能包含多个IP地址，通常第一个是最原始的客户端IP
        ip_address = x_forwarded_for.split(',')[0]
    return ip_address